<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class RefundReason extends Base {
    public function index(){
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('name')){
                $map['a.name'] = input('name');
            }
            $count = Db::name('refund_reason')->alias('a')->where($map)->count();
            $field = '*';
            $list = Db::name('refund_reason')
                ->alias('a')
                ->field($field)
                ->where($map)
                ->order('sort desc,id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }

    public function edit()
    {
        $params = input();
        $id = input('id')?:'';
        if(request()->isPost()){
            $data = [];
            $data['name'] = $params['name'];
            $data['type'] = $params['type'];
            $data['sort'] = $params['sort'];
            $data['status'] = 1;
            $data['w_time'] = getCurTime();
            if($id){
                Db::name('refund_reason')->where('id',$id)->update($data);
            }else{
                Db::name('refund_reason')->insert($data);
            }
            return json(['status'=>true,'msg'=>'操作成功']);
        }else{
            if($id){
                $info = Db::name('refund_reason')->where('id',$id)->find();
            }else{
                $info = [];
            }
            View::assign('info',$info);
            return View::fetch();
        }
    }
    public function del(){
        $params = input();
        $ids = input('ids/a')?:'';
        Db::name('refund_reason')->whereIn('id',$ids)->delete();

        return json(['status'=>true,'msg'=>'操作成功']);
    }
}